package com.example.mytoolbar;

import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private Toolbar toolbar;
    private CardView cardView;
    private FloatingActionButton Myfab;
    private CoordinatorLayout coordinatorLayout;
    private int count=0;

    private LinearLayout bottomSheet;
    private BottomSheetBehavior<LinearLayout> bottomSheetBehavior;
    private boolean isOpen;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        toolbar = findViewById(R.id.toolbar);
        cardView = findViewById(R.id.cardview);
        Myfab = findViewById(R.id.fab);
        coordinatorLayout = findViewById(R.id.cl_root);
        bottomSheet = findViewById(R.id.bottom_sheet);
        //监听BottomSheet
        bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
        bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
            @Override
            public void onStateChanged(@NonNull View view, int i) {
                Toast.makeText(MainActivity.this,"i ="+i, Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onSlide(@NonNull View view, float v) {

            }
        });

        //没有用这个方法的话，没有bar条
        setSupportActionBar(toolbar);

        //3.flaot 按钮点击事件
        Myfab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isOpen){
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
                }else{
                    bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
                }
                isOpen=!isOpen;

            }
        });


        //点击cardview事件
        cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                runAnima();
            }
        });
    }


    //cardview点击变化
    private void runAnima() {

        cardView.animate().translationY(20+count).setDuration(1000).start();
        count+=10;
    }
    //菜单加载
    @Override
    public boolean onCreatePanelMenu(int featureId, Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main,menu);
        return true;
    }

    //navigation图标的监听  点击就返回了   菜单点击事件
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        String msg ="";
        switch (item.getItemId()){
            case R.id.action_jia:
                msg+="点击了添加";
                break;
            case R.id.action_suo:
                msg+="点击了搜索";
                break;
            case R.id.list_item1:
                msg+="点击了item1";
                break;
            case R.id.list_item2:
                msg+="点击了item2";
                break;
            case android.R.id.home:
                Log.e("onOptionsItemSelected",item.toString());
                finish();
                break;
        }
        if (!msg.equals("")){
            Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
        }
        return super.onOptionsItemSelected(item);
    }



}
